<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        var d = new Date();
        function formatDate(date) {    
            var d = new Date(),      
                month = '' + (d.getMonth() + 1),      
                day = '' + d.getDate(),      
                year = d.getFullYear();    
            alert(d.toDateString());  
            if (month.length < 2) month = '0' + month;    
                if (day.length < 2) day = '0' + day;      
                return [year, month, day].join('-');  
        }
        console.log(formatDate(d.toDateString())); 
    </script>
    
</body>
</html>